package com.framework.wz.wutong.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 图片压缩
 */
public class ImageCompressionUtils {

    /**
     * 分辨率压缩
     *
     * @param context
     * @param pic
     * @return
     */

    // 获取本地资源文件
    public static Bitmap getResourceBitMap(Context context, int pic) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // 当前只为获取图片的边界大小
        BitmapFactory.decodeResource(context.getResources(), pic, options);
        int    outHeight   = options.outHeight;
        int    outWidth    = options.outWidth;
        String outMimeType = options.outMimeType;
        options.inJustDecodeBounds = false;
        options.inSampleSize = caculateSampleSize(options, 1080, 1920);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), pic, options);
        return bitmap;
    }

    // 获取sd卡图片
    public static Bitmap getFileBitMap(String path) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // 当前只为获取图片的边界大小
        BitmapFactory.decodeFile(path);
        int    outHeight   = options.outHeight;
        int    outWidth    = options.outWidth;
        String outMimeType = options.outMimeType;
        options.inJustDecodeBounds = false;
        options.inSampleSize = caculateSampleSize(options, 1080, 1920);
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        return bitmap;
    }

    // 获取网络图片
    public static Bitmap getStreamBitMap(InputStream inputStream) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true; // 当前只为获取图片的边界大小
        BitmapFactory.decodeStream(inputStream);
        int    outHeight   = options.outHeight;
        int    outWidth    = options.outWidth;
        String outMimeType = options.outMimeType;
        options.inJustDecodeBounds = false;
        options.inSampleSize = caculateSampleSize(options, 1080, 1920);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        return bitmap;
    }

    private static int caculateSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int sampleSize = 1;
        int picWidth   = options.outWidth;
        int picHeight  = options.outHeight;
        if (picWidth > reqWidth || picHeight > reqHeight) {
            int halfPicWidth  = picWidth / 2;
            int halfPicHeight = picHeight / 2;
            while (halfPicWidth / sampleSize > reqWidth || halfPicHeight / sampleSize > reqHeight) {
                sampleSize *= 2;
            }
        }
        return sampleSize;
    }

    /**
     * 质量压缩
     *
     * @param format  图片格式 jpeg,png,webp
     * @param quality 图片的质量,0-100,数值越小质量越差
     */
    public static void compress(Bitmap.CompressFormat format, int quality) {
        File                  sdFile       = Environment.getExternalStorageDirectory();
        File                  originFile   = new File(sdFile, "originImg.jpg");
        Bitmap                originBitmap = BitmapFactory.decodeFile(originFile.getAbsolutePath());
        ByteArrayOutputStream bos          = new ByteArrayOutputStream();
        originBitmap.compress(format, quality, bos);
        try {
            FileOutputStream fos = new FileOutputStream(new File(sdFile, "resultImg.jpg"));
            fos.write(bos.toByteArray());
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
